package fr.ssd.sdl.loader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

/**
 * This is an abstract class for reading sql statements from a file. A specific
 * implementation may extend this class and override the {@code
 * loadSqlStatement()} method.
 * 
 * @author Sylvain Sicard
 * 
 */
public abstract class AbstractSqlLoader implements Loader {

	protected static final String sourceFileNameKey = "sourceFileName";
	protected static final String lineCommentToken = "--";

	/**
	 * The Reader use to read statements from the input file.
	 */
	protected BufferedReader r;

	/**
	 * @param hints
	 *            the map is required to contain the entry {@code
	 *            <"sourceFileName","path/to/source.sql">}
	 */
	@Override
	public void init(Map<String, ?> hints) throws LoaderException {
		String fileName = (String) hints.get(sourceFileNameKey);
		try {
			this.r = new BufferedReader(new FileReader(fileName));
		} catch (FileNotFoundException e) {
			throw new LoaderException(e);
		}
	}

	@Override
	public void terminate() throws LoaderException {
		try {
			r.close();
		} catch (IOException e) {
			throw new LoaderException(e);
		}
	}

}